package ar.com.cubika.trainees.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ar.com.cubika.trainees.bean.transportes.Chofer;

public class ChoferesDAO extends AbstractMySQLDAO {

	// Singleton
	private static ChoferesDAO instance = new ChoferesDAO();

	private ChoferesDAO() {
	}

	public static ChoferesDAO getInstance() {
		return instance;
	}

	public List<Chofer> getAll() {
		List<Chofer> choferes = new ArrayList<Chofer>();

		try {

			Statement stmt = getNewStatement();

			ResultSet rs = stmt.executeQuery("SELECT * FROM CHOFER");

			while (rs.next()) {
				Chofer unChofer = new Chofer(rs.getString("nombre"),
						rs.getString("apellido"), rs.getString("dni"),
						rs.getInt("edad"), rs.getString("cuil"), rs.getString(
								"sexo").charAt(0), rs.getBoolean("disponible"));
				choferes.add(unChofer);

			}
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return choferes;
	}

	public void insert(Chofer unChofer) {

		String nombre = unChofer.getNombre();
		String apellido = unChofer.getApellido();
		String dni = unChofer.getDni();
		int edad = unChofer.getEdad();
		String cuil = unChofer.getCuil();
		char sexo = unChofer.getSexo();
		boolean disponible = unChofer.isDisponible();

		try {

			Statement stmt1 = getNewStatement();

			String query = "INSERT INTO CHOFER (nombre,apellido,sexo,dni,cuil,edad,disponible) VALUES ('"
					+ nombre
					+ "','"
					+ apellido
					+ "','"
					+ sexo
					+ "','"
					+ dni
					+ "','" + cuil + "'," + edad + "," + disponible + ");";

			stmt1.executeUpdate(query);

			stmt1.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Chofer deleteChofer(String dni) {

		try {
			Statement stmt1 = getNewStatement();

			String query = "DELETE FROM CHOFER WHERE DNI ='" + dni + "';";

			stmt1.executeUpdate(query);

			stmt1.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

}
